import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/ht_common_widgets.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';

import '../login_page/constant.dart';
import '../mine/contentType.dart';
import 'product_bootom_logic.dart';

class ProductBottomSheet extends StatelessWidget {
  String? logo = '';
  String? name = '';
  String? content = '';

  ProductBottomSheet({
    Key? key,
    this.logo,
    this.name,
    this.content,
  }) : super(key: key);

  static void show(BuildContext context, {String? logo, String? name, String? content}) {
    showModalBottomSheet(
        backgroundColor: Colors.transparent,//重点
        context: context,
        isScrollControlled: false,
        enableDrag: false,
        isDismissible: false,
        builder: (context) {
          return ProductBottomSheet(
            logo: logo,
            name: name,
            content: content,
          );
        });
  }

  final logic = Get.put(ProductBottomLogic());

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      // height: (ScreenUtil().screenHeight * 0.5).w + ScreenUtil().bottomBarHeight,
      // height: (ScreenUtil().screenHeight * 0.65).w + ScreenUtil().bottomBarHeight,
      child: GetBuilder<ProductBottomLogic>(
        assignId: true,
        builder: (logic) {
          return Column(
            children: [
              Container(
                height: 460.w + ScreenUtil().bottomBarHeight,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
                child: Column(
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 15.w, left: 16.w, bottom: 20.w),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset(
                          'images/icon_complete.png',
                          width: 20.w,
                          height: 20.w,
                        ),
                        SizedBox(
                          width: 10.w,
                        ),
                        Text(
                          '恭喜您，已通过初审',
                          style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                    Container(
                      height: 105.w,
                      width: double.infinity,
                      margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 10.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorFFFFFF,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(Radius.circular(5.0)),
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            '${logic.amount}元',
                            style: TextStyleUtils.styleFFECE9(fontSize: 32.sp, fontWeight: FontWeight.bold),
                          ),
                          Text(
                            '您的借款将由合作方${logic.content}提供',
                            style: TextStyleUtils.style666666(fontSize: 12.sp),
                          )
                        ],
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(
                        left: 15.w,
                        right: 15.w,
                        bottom: 15.w,
                      ),
                      padding: EdgeInsets.only(left: 8.w, right: 8.w, top: 10.w, bottom: 10.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorF8F8F8,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(Radius.circular(5.0)),
                      ),
                      height: 150.w,
                      child: MediaQuery.removePadding(
                        removeTop: true,
                        context: Get.context!,
                        child: ListView(
                          shrinkWrap: true,
                          children: [
                            HtmlWidget(
                              content ?? '',
                            ),
                          ],
                        ),
                      ),
                    ),
                    ClickWidget(
                      onTap: () {
                        if(logic.id!=0){
                          logic.addLoanRecord(logic.id!, ShowPage.result);
                        }
                        Get.to(WebViewCommon(
                          logic.popLinkUrl ?? '',
                        ));
                      },
                      child: Container(
                        width: double.infinity,
                        height: 48.w,
                        alignment: const Alignment(0, 0),
                        margin: EdgeInsets.only(right: 16.w, left: 16.w, top: 7.w),
                        decoration: BoxDecoration(
                          color: ColorManager.colorD93C3A,
                          //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                          borderRadius: BorderRadius.all(Radius.circular(25.0)),
                        ),
                        child: Text(
                          '申请提现',
                          style: TextStyleUtils.styleFFFFFF(fontSize: 16.sp, fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                    ClickWidget(
                      onTap: () {
                        Get.back();
                      },
                      child: Container(
                        margin: EdgeInsets.only(top: 20.w),
                        child: Text(
                          '放弃借款',
                          style: TextStyleUtils.style666666(fontSize: 16.sp, fontWeight: FontWeight.bold),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              Visibility(
                visible: false,
                child: Container(
                  color: ColorManager.colorFFFFFF,
                  padding: EdgeInsets.symmetric(horizontal: 15.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: EdgeInsets.symmetric(vertical: 20.w),
                        child: Row(
                          children: [
                            HtImage(
                              icon: true,
                              url: logic.imgUrl ?? '',
                              width: 24.w,
                              height: 24.w,
                              radius: 2,
                              placeholder: 'images/logo.png',
                              errImage: 'images/logo.png',
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Text(
                              logic.content ?? '',
                              style: TextStyleUtils.style000000(fontSize: 16.sp, fontWeight: FontWeight.w400),
                            ),
                            SizedBox(
                              width: 30.w,
                            ),
                            Image.asset(
                              'images/icon_complete.png',
                              width: 13.w,
                              height: 13.w,
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Text(
                              '恭喜您，已通过初审',
                              style: TextStyleUtils.style000000(fontSize: 12.sp, fontWeight: FontWeight.w400),
                            ),
                          ],
                        ),
                      ),
                      Text(
                        '借款金额(元)',
                        style: TextStyleUtils.style000000(fontSize: 12.sp, fontWeight: FontWeight.w400),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 8.w, bottom: 11.w),
                        child: Text(
                          '${logic.randomElement}',
                          style: TextStyleUtils.style000000(fontSize: 30.sp, fontWeight: FontWeight.bold),
                        ),
                      ),
                      Container(
                        width: double.infinity,
                        height: 1.w,
                        color: ColorManager.colorEEEEEE,
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 7.w, bottom: 14.w),
                        child: Text(
                          '可借额度：500-20000元，实际放款金额由机构综合评估确定',
                          style: TextStyleUtils.style999999(fontSize: 12.sp),
                        ),
                      ),
                      Container(
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            Expanded(
                              child: Text(
                                '贷款期数',
                                style: TextStyleUtils.style000000(fontSize: 14.sp),
                              ),
                            ),
                            Container(
                              height: 28.w,
                              width: 55.w,
                              alignment: const Alignment(0, 0),
                              decoration: BoxDecoration(
                                  color: ColorManager.colorFFFFFF,
                                  //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                                  borderRadius: BorderRadius.all(Radius.circular(25.w)),
                                  border: Border.all(width: 1.w, color: ColorManager.colorD93C3A)),
                              child: Text(
                                '3个月',
                                style: TextStyleUtils.styleFFECE9(fontSize: 12.sp, fontWeight: FontWeight.bold),
                              ),
                            ),
                            Container(
                              height: 28.w,
                              width: 55.w,
                              margin: EdgeInsets.symmetric(horizontal: 8.w),
                              alignment: const Alignment(0, 0),
                              decoration: BoxDecoration(
                                  color: ColorManager.colorFFFFFF,
                                  //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                                  borderRadius: BorderRadius.all(Radius.circular(25.w)),
                                  border: Border.all(width: 1.w, color: ColorManager.colorD93C3A)),
                              child: Text(
                                '6个月',
                                style: TextStyleUtils.styleFFECE9(fontSize: 12.sp, fontWeight: FontWeight.bold),
                              ),
                            ),
                            Container(
                              height: 28.w,
                              width: 55.w,
                              alignment: const Alignment(0, 0),
                              decoration: BoxDecoration(
                                color: ColorManager.colorD93C3A,
                                //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                                borderRadius: BorderRadius.all(Radius.circular(25.w)),
                              ),
                              child: Text(
                                '12个月',
                                style: TextStyleUtils.styleFFFFFF(fontSize: 12.sp, fontWeight: FontWeight.bold),
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(
                        height: 28.w,
                      ),
                      Container(
                          height: 60.w,
                          margin: EdgeInsets.only(bottom: 16.w),
                          decoration: BoxDecoration(
                            color: ColorManager.colorF4F6FF,
                            //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                            borderRadius: BorderRadius.all(Radius.circular(5.0)),
                          ),
                          child: Container(
                            margin: EdgeInsets.only(top: 15.w, left: 14.w,right: 14.w),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Column(
                                      children: [
                                        Text(
                                          '35000',
                                          style: TextStyleUtils.style000000(
                                            fontSize: 15.sp,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        Text(
                                          '最高额度(元)',
                                          style: TextStyleUtils.style999999(fontSize: 12.sp),
                                        )
                                      ],
                                    ),
                                    Column(
                                      children: [
                                        Text(
                                          '0.7%',
                                          style: TextStyleUtils.style000000(
                                            fontSize: 15.sp,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        Text(
                                          '平均月利率',
                                          style: TextStyleUtils.style999999(fontSize: 12.sp),
                                        )
                                      ],
                                    ),
                                    Column(
                                      children: [
                                        Text(
                                          '1-12月',
                                          style: TextStyleUtils.style000000(
                                            fontSize: 15.sp,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        Text(
                                          '最高可借(期)',
                                          style: TextStyleUtils.style999999(fontSize: 12.sp),
                                        )
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          )),
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: 35.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            buildColumn('images/icon_audit1.png', '在线申请'),
                            Image.asset(
                              'images/icon_arrow.png',
                              width: 28.w,
                              height: 28.w,
                            ),
                            buildColumn('images/icon_audit2.png', '风控审核'),
                            Image.asset(
                              'images/icon_arrow.png',
                              width: 28.w,
                              height: 28.w,
                            ),
                            buildColumn('images/icon_audit3.png', '机构审核'),
                            Image.asset(
                              'images/icon_arrow.png',
                              width: 28.w,
                              height: 28.w,
                            ),
                            buildColumn('images/icon_audit4.png', '机构放款'),
                          ],
                        ),
                      ),
                      SizedBox(
                        height: 30.w,
                      ),
                      Container(
                        alignment: Alignment.center,
                        child: Text(
                          '根据您的资质评估，该产品通过率最高',
                          style: TextStyleUtils.style999999(fontSize: 12.sp),
                        ),
                      ),
                      ClickWidget(
                        onTap: () {
                          if(logic.isAgreeUserPrivary){
                            if(logic.id!=0){
                              logic.addLoanRecord(logic.id!, ShowPage.result);
                            }
                            Get.to(WebViewCommon(
                              logic.popLinkUrl ?? '',
                            ));
                          }else{
                            ToastUtils.showToast("请阅读并勾选协议");
                          }
                        },
                        child: Container(
                          width: double.infinity,
                          height: 48.w,
                          alignment: Alignment.center,
                          margin: EdgeInsets.symmetric(vertical: 20.w),
                          decoration: BoxDecoration(
                            color: ColorManager.color1D52FF,
                            //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                            borderRadius: BorderRadius.all(
                              Radius.circular(5.w),
                            ),
                          ),
                          child: Text(
                            '立即提现',
                            style: TextStyleUtils.styleFFFFFF(
                              fontSize: 18.sp,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                      ClickWidget(
                        onTap: () {
                          Get.back();
                        },
                        child: Container(
                          alignment: Alignment.center,
                          margin: EdgeInsets.only(bottom: 20.w),
                          child: Text(
                            '放弃借款',
                            style: TextStyleUtils.style000000(fontSize: 16.sp),
                          ),
                        ),
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(right: 5.w),
                            child: GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap: () {
                                logic.updateUserPrivary();
                              },
                              child: Container(
                                width: 16.w,
                                height: 16.w,
                                margin: const EdgeInsets.only(top: 4),
                                alignment: Alignment.center,
                                child: logic.isAgreeUserPrivary
                                    ? Image.asset(
                                        "images/ic_agreement_checked.png",
                                        width: 11.w,
                                        height: 11.w,
                                      )
                                    : Image.asset(
                                        "images/ic_agreement_unchecked.png",
                                        width: 11.w,
                                        height: 11.w,
                                      ),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Text.rich(
                              TextSpan(
                                children: <InlineSpan>[
                                  TextSpan(
                                      text: "我已阅读并同意",
                                      style: TextStyleUtils.style999999(fontSize: 11.sp),
                                      recognizer: TapGestureRecognizer()..onTap = () {}),
                                  // TextSpan(
                                  //   text: "《使用条款》",
                                  //   style: TextStyle(color: ColorManager.color437EFF, fontSize: 11.sp, height: 1.5.w),
                                  //   // 设置点击事件
                                  //   recognizer: TapGestureRecognizer()
                                  //     ..onTap = () {
                                  //       logic.getUserInfo(ContentType.thirdUserSharePolicy);
                                  //     },
                                  // ),
                                  // TextSpan(
                                  //   text: "《隐私协议》",
                                  //   style: TextStyle(color: ColorManager.color437EFF, fontSize: 11.sp, height: 1.5.w),
                                  //   // 设置点击事件
                                  //   recognizer: TapGestureRecognizer()
                                  //     ..onTap = () {
                                  //       logic.getUserInfo(ContentType.newPrivacyPolicy);
                                  //     },
                                  // ),
                                  TextSpan(
                                    text: "《个人信息共享协议》",
                                    style: TextStyle(color: ColorManager.color437EFF, fontSize: 11.sp, height: 1.5.w),
                                    // 设置点击事件
                                    recognizer: TapGestureRecognizer()
                                      ..onTap = () {
                                        Get.toNamed(BaseRouterConfig.agreement, arguments: {'name': '', 'content': content});
                                      },
                                  ),
                                ],
                              ),
                              style: TextStyle(
                                color: ColorManager.color999999,
                                fontSize: 13.sp,
                                height: 1.5,
                              ),
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  Column buildColumn(String image, String title) {
    return Column(
      children: [
        Image.asset(
          image,
          width: 30.w,
          height: 28.w,
        ),
        Text(
          title,
          style: TextStyleUtils.style666666(fontSize: 9.w),
        ),
      ],
    );
  }


}
